package cn.lg.soar.system.biz.modules.auth.service;

import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.system.biz.config.SystemCacheKey;
import cn.lg.soar.system.biz.modules.auth.entity.MenuPermitRel;
import cn.lg.soar.system.biz.modules.auth.mapper.MenuPermitRelMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author luguoxiang
 * @date 2022/4/10
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class MenuPermitRelService extends ServiceImpl<MenuPermitRelMapper, MenuPermitRel> {

    /**
     * 根据菜单id获取权限
     * @param menuId
     * @return
     */
    @Cacheable(value = SystemCacheKey.MENUID_TO_PERMITS, key = "#p0", cacheManager = "remote")
    public List<Integer> getPermitIds(Integer menuId) {
        return listObjs(
                Wrappers.<MenuPermitRel>lambdaQuery()
                        .select(MenuPermitRel::getPermitId)
                        .eq(MenuPermitRel::getMenuId, menuId),
                x -> Integer.parseInt(x.toString())
        );
    }

    /**
     * 设置菜单权限
     * @param menuId
     * @param permits
     */
    @Caching(evict = {
            @CacheEvict(value = SystemCacheKey.MENUID_TO_PERMITS, key = "#p0", cacheManager = "remote"),
            @CacheEvict(value = SystemCacheKey.ROLEID_TO_PERMITS, allEntries = true)
    })
    @Transactional(rollbackFor = RuntimeException.class)
    public void setMenuPermit(Integer menuId, Collection<Integer> permitIds) {
        // 清空旧值
        clearByMenu(menuId);
        // 批量保存新值
        if (CollectionUtils.isEmpty(permitIds)) {
            return;
        }
        List<MenuPermitRel> collect = permitIds.stream()
                .distinct()
                .map(x -> {
                    MenuPermitRel data = new MenuPermitRel();
                    data.setMenuId(menuId);
                    data.setPermitId(x);
                    return data;
                })
                .collect(Collectors.toList());
        AssertUtil.isTrue(saveBatch(collect), "保存菜单权限失败");
    }

    @Caching(evict = {
            @CacheEvict(value = SystemCacheKey.MENUID_TO_PERMITS, key = "#p0", cacheManager = "remote"),
            @CacheEvict(value = SystemCacheKey.ROLEID_TO_PERMITS, allEntries = true)
    })
    public void clearByMenu(Integer menuId) {
        this.remove(Wrappers.<MenuPermitRel>lambdaQuery().eq(MenuPermitRel::getMenuId, menuId));
    }

    public void clearByPermit(Integer permitId) {
        this.remove(Wrappers.<MenuPermitRel>lambdaQuery().eq(MenuPermitRel::getPermitId, permitId));
    }
}
